package com.hn658.base.message.account.dao.redis.impl;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.hn658.base.message.account.dao.redis.IAccountInfoRedisDAO;
import com.hn658.base.message.account.entity.AccountInfoEO;
import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;

/**
 * 用户Redis DAO
 * 
 * @ClassName:UserRedisDAOImpl
 * @author ztjie
 * @date 2014年7月1日 下午3:23:29
 */
@Repository
public class AccountInfoRedisDAOImpl extends AbstractRedisDAO<AccountInfoEO> implements
		IAccountInfoRedisDAO {

	@Autowired
	@Qualifier("redisTemplate")
	@Override
	public void setTemplate(StringRedisTemplate template) {
		super.template = template;
	}

	@Override
	public void save(AccountInfoEO accountInfo) {
		if (accountInfo != null && accountInfo.getId() != null) {
			// 从redis中获取对应uid的UserInfo
			BoundHashOperations<String, String, String> userOps = template
					.boundHashOps(RedisKeyHelper.accountId(String
							.valueOf(accountInfo.getId())));
			// 保存所有用户信息
			userOps.put("appId", accountInfo.getAppId());
			userOps.put("id", String.valueOf(accountInfo.getId()));
			userOps.put("accountId", accountInfo.getAccountId());
			userOps.put("name", accountInfo.getName());
			userOps.put("portraitUri", accountInfo.getPortraitUri());
			if (accountInfo.getCreateTime() != null) {
				userOps.put("createTime",
						String.valueOf(accountInfo.getCreateTime().getTime()));
			}
			if (accountInfo.getLastUpdateTime() != null) {
				userOps.put("lastUpdateTime",
						String.valueOf(accountInfo.getLastUpdateTime().getTime()));
			}
		}
	}

	@Override
	public AccountInfoEO getAccountById(Long accountId) {
		AccountInfoEO userInfo = null;
		if (accountId != null) {
			BoundHashOperations<String, String, String> userOps = template
					.boundHashOps(RedisKeyHelper.accountId(String
							.valueOf(accountId)));
			Map<String, String> entries = userOps.entries();
			if (entries != null && !entries.isEmpty()) {
				userInfo = mapper.fromHash(entries);
			}
		}
		return userInfo;
	}

	@Override
	public AccountInfoEO getAccountByAccessToken(String accessToken) {
		AccountInfoEO accountInfo = null;
		String accountId = valueOps.get(RedisKeyHelper.accountIdByAccessToken(accessToken));
		if (StringUtils.isNotBlank(accountId)) {
			accountInfo = this.getAccountById(Long.valueOf(accountId));
		}
		return accountInfo;
	}

	@Override
	public void delete(AccountInfoEO account) {
		// 删除用户信息
		if (account != null && account.getId() != null
				&& account.getLoginAccount() != null) {
			Set<String> deleteKeys = new HashSet<String>();
			String accessToken = valueOps.get(RedisKeyHelper
					.accessTokenByAccountId(String.valueOf(account.getId())));
			if (StringUtils.isNotBlank(accessToken)) {
				// 删除AccessToken To Uid
				deleteKeys.add(RedisKeyHelper.accessTokenByAccountId(String
						.valueOf(account.getId())));
				// 删除Uid To AccessToken
				deleteKeys.add(RedisKeyHelper.accountIdByAccessToken(accessToken));
			}
			// 删除Uid To UserInfo
			deleteKeys.add(RedisKeyHelper.accountId(String.valueOf(account
					.getId())));
			template.delete(deleteKeys);
		}
	}

	@Override
	public void setAccessToken(String accountId, String accessToken) {

		// 删除原先的accessToken
		String origAccessToken = valueOps.get(RedisKeyHelper.accessTokenByAccountId(accountId));
		if (StringUtils.isNotEmpty(origAccessToken)) {// 删除accessToken对应的uid
			template.delete(RedisKeyHelper.accountIdByAccessToken(origAccessToken));
		}
		// 创建authkey
		valueOps.set(RedisKeyHelper.accessTokenByAccountId(accountId), accessToken);
		valueOps.set(RedisKeyHelper.accountIdByAccessToken(accessToken), accountId);
	}

	@Override
	public void removeAccessToken(String accountId) {
		String accessToken = valueOps.get(RedisKeyHelper.accessTokenByAccountId(accountId));
		template.delete(RedisKeyHelper.accountIdByAccessToken(accessToken));
		template.delete(RedisKeyHelper.accountId(accountId));
	}

}